package cn.edu.zzuli.nothinglink.controller;


import cn.edu.zzuli.nothinglink.anno.CurrentUser;
import cn.edu.zzuli.nothinglink.entity.LiveInfo;
import cn.edu.zzuli.nothinglink.entity.R;
import cn.edu.zzuli.nothinglink.entity.SrsCallBackData;
import cn.edu.zzuli.nothinglink.service.LiveInfoService;
import cn.edu.zzuli.nothinglink.vo.StartLiveVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suzyyy
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/live")
public class LiveInfoController {

    @Autowired
    LiveInfoService liveInfoService;

    @PostMapping("/srs/callback")
    public Integer doCallback(@RequestBody SrsCallBackData callBackData) {
        String action = callBackData.getAction();

        switch (action) {
            case "on_connect":
                System.out.println("on_connect");
                return liveInfoService.onConnect(callBackData);
            case "on_publish":
                System.out.println("on_publish");
                return liveInfoService.onPublish(callBackData);
            case "on_close":
                System.out.println("on_close");
                return liveInfoService.onClose(callBackData);
            default:
                return 0;
        }

//        if(callBackData.getAction().equals("on_connect")) {
//            return liveInfoService.onConnect(callBackData);
//        }else if (callBackData.getAction().equals("on_publish")) {
//            return liveInfoService.onPublish(callBackData);
//        }else if (callBackData.getAction().equals("on_close")){
//            return liveInfoService.onClose(callBackData);
//        }else {
//            return 0;
//        }
    }


    @PostMapping("/create")
    public R createMyLiveRoom(@CurrentUser Integer teaId) {
        if (teaId == null) return R.error().add("error", "请登录");
        //新建一个直播间
        return liveInfoService.createLiveRoom2TheTeaId(teaId);
    }

    @GetMapping("/info")
    public R getMyLiveRoomInfo(@CurrentUser Integer teaId) {
        System.out.println(teaId);
        if (teaId == null) return R.error().add("error", "请登录");
        return R.success().add("liveInfo", liveInfoService.getOne(
                new LambdaQueryWrapper<LiveInfo>().eq(LiveInfo::getLiveBelongTeaId, teaId)
        ));
    }

    @GetMapping("/init")
    public R getMyLiveInit(@CurrentUser Integer teaId) {
        if (teaId == null) return R.error().add("error", "请登录");
        return R.success().add("initInfo", liveInfoService.getInitInfo(teaId));
    }

    @PutMapping("/start")
    public R startLive(@CurrentUser Integer teaId, @RequestBody @Valid StartLiveVo vo) {
        if (teaId == null) return R.error().add("error", "请登录");
        if (liveInfoService.startLiveAndSetLiveInfo(teaId, vo)) return R.success();
        return R.error().add("error", "有点错误导致开启失败，可能网络有点问题。");
    }

    @DeleteMapping("/close/{stream}")
    public R closeTheLive(@PathVariable String stream) {
        if (liveInfoService.stopLiveAndRemoveKeyFromRedis(stream)) return R.success();
        return R.error().add("error", "请先关闭推流！！！如果已关闭那么后台网络有点问题。请稍后再试。");
    }

    @GetMapping("/is_start/{courseId}/{stream}")
    public R theCourseIsStart(@PathVariable("stream") String stream,
                              @PathVariable("courseId") Integer courseId) {
        return liveInfoService.getTheCourseLiveIsStart(courseId, stream);
    }

}

